package Operation;

import Student.Student;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-16
 * Time: 20:36
 */
public class AddOperation implements IOperation{

    @Override
    public void work(TreeMap<Student,String> treeMap) {
        System.out.println("数据录入");
        Scanner scanner=new Scanner(System.in);
        String name=null;
        String id=null;
        int age=0;
        String tel=null;
        String sex=null;
        while(true){
            System.out.println("请输入学号，学号为10个数字");
            id=scanner.nextLine();
            if(checkId(id)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入姓名");
            name=scanner.nextLine();
            if(checkName(name)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入性别");
            sex=scanner.nextLine();
            if(checkSex(sex)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入年龄");
            age=scanner.nextInt();
            if(checkAge(age)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入手机号");
           Scanner scanner1=new Scanner(System.in);
           tel=scanner1.nextLine();
            if(checkTel(tel)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }

            break;
        }

        treeMap.put(new Student(id,name,age,tel,sex),id);

    }
    public boolean checkId(String str){
        return str.matches("^[0-9]{10}$");
    }
    public boolean checkName(String str){
        return str.matches("^[\u4E00-\u9FA5]{2,4}$");
    }
    public boolean checkAge(int age){
       if(age>=5&&age<=100)
           return true;
       else
           return false;
    }
    public boolean checkTel(String str){
        return str.matches("^[0-9]{11}$");
    }
    public boolean checkSex(String str){
       if(str.length()!=1)
           return false;
        if(str.equals("男")||str.equals("女")){
           return true;
       }
       else
           return false;
    }

}
